﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Entity;
public partial class admin_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpCookie Cookie = CookiesHelper.GetCookie("UserInfo");
            if (Cookie != null)
            {
                this.txtUserName.Text = Cookie.Values["uName"];
            }
        }
    }
    /// <summary>
    /// 登陆
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string loginName = txtUserName.Text.Trim();
        if (txtUserName.Text == "")
        {
            meg.InnerHtml = "用户名不能为空！";
            txtUserName.Focus();
        }
        else if (txtPassWord.Text == "")
        {
            meg.InnerHtml = "密码不能为空！";
            txtPassWord.Focus();
        }
        else
        {
            List<SysUser> users = SysUserDAL.Instance.CheckUserLogin(loginName);
            if (users.Count > 0)
            {
                if (ClassMD5.EncryptString(txtPassWord.Text) == users[0].Pass_word)
                {
                    LoginSessionEntity login = new LoginSessionEntity();
                    login.LoginName = loginName;
                    login.UserName = users[0].User_name;
                    login.Email = users[0].Email;

                    SysUserDAL.Instance.UpdateLoingInfo(users[0].ID.ToString(), Request.UserHostAddress);
                    
                    if (chkSaveUser.Checked == true)
                    {
                        HttpCookie Cookie = CookiesHelper.GetCookie("UserInfo");
                        if (Cookie == null)
                        {
                            Cookie = new HttpCookie("UserInfo");
                            Cookie.Values.Add("uName", loginName);
                            //设置Cookie过期时间
                            Cookie.Expires = DateTime.Now.AddDays(15);
                            CookiesHelper.AddCookie(Cookie);
                        }
                        else if (!Cookie.Values["uName"].Equals(loginName))
                            CookiesHelper.SetCookie("UserInfo", "uName", loginName);

                    }
                    Session.Add("LoginUser", login);
                    Response.Write("<script>parent.location.href='index.aspx';</script>");

                }
                else
                {
                    if (txtPassWord.Text == "")
                    {
                        meg.InnerHtml = "密码不能为空！";
                        txtPassWord.Focus();
                    }
                    else
                    {
                        txtPassWord.Text = "";
                        meg.InnerHtml = "密码错误，请重新输入！";
                        txtPassWord.Focus();
                    }
                }

            }
            else
            {
                meg.InnerHtml = "用户名不存在，请联系管理员！";
                txtUserName.Text = "";
                txtPassWord.Text = "";
                txtUserName.Focus();
            }
        }
    }
}